<template>
  <page-header-wrapper :title="false">
    <el-card class="box-card">
      <div slot="header" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :span="6">
              <a-form-item label="所属分类名称">
                <a-input v-model="queryParam.categoryName" placeholder="请输入:所属分类名称" />
              </a-form-item>
            </a-col>

            <a-col :span="12">
              <a-button style="margin-right: 8px" type="primary" @click="query">查询</a-button>
              <a-button style="margin-right: 8px" @click="reset">重置</a-button>
              <a-button icon="plus" style="margin-right: 8px" type="primary" @click="handleAdd">新建</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column label="分类名称" prop="categoryName" align="center" />
        <el-table-column label="分类分值" prop="categoryScore" align="center" />
        <!-- <el-table-column width="150" label="状态" align="center" >
          <template slot-scope="scope">
            <el-tag v-if="parseInt(scope.row.state) === 1">激活</el-tag>
            <el-tag v-if="parseInt(scope.row.state) === 2" type="danger">冻结</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column label="排序号" prop="sortOrder" align="center" />
        <el-table-column label="创建时间" prop="createTime" align="center" />
        <el-table-column label="更新时间" prop="updateTime" align="center" />
        <el-table-column width="150" fixed="right" align="center" label="操作">
          <template slot-scope="scope">
            <el-link :underline="false" type="primary" @click="handleEdit(scope.row)">编辑</el-link>
            <el-link :underline="false" style="margin-left:10px;" type="primary"
              @click="handleLabel(scope.row)">标签</el-link>
          </template>
        </el-table-column>
      </el-table>

      <div style="display: flex;justify-content: flex-end;margin: 20px 0 0;">
        <el-pagination :current-page="queryParam.page" :page-size="queryParam.limit" :page-sizes="[10, 20, 50, 100]"
          :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
          @current-change="handleCurrentChange">
        </el-pagination>
      </div>
    </el-card>

    <a-modal :title="modal.title" :visible="modal.visible" maskClosable width="600px" @cancel="modal.visible = false"
      @ok="handleOk">
      <a-form :form="modal.form" :label-col="{ span: 3 }" :wrapper-col="{ span: 16 }">

        <a-form-item label="分类名称">
          <a-input v-model="modal.form.categoryName" placeholder="请输入:分类名称" />
        </a-form-item>

        <a-form-item label="分类分值">
          <a-input-number :min="0" :precision="0" v-model="modal.form.categoryScore" placeholder="分类分值"
            :style="{ width: '100%', textAlign: 'center', display: 'inline-block' }" />
        </a-form-item>

        <a-form-item label="排序号">
          <a-input-number :min="1" :precision="0" v-model="modal.form.sortOrder" placeholder="排序号"
            :style="{ width: '100%', textAlign: 'center', display: 'inline-block' }" />
        </a-form-item>

      </a-form>
    </a-modal>

    <!-- 标签 -->
    <a-modal :title="modal.title" :visible="modal.visible1" destroyOnClose maskClosable width="1000px"
      @cancel="modal.visible1 = false" @ok="modal.visible1 = false">
      <survey-category-label :id="modal.currentId"></survey-category-label>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
  import {
    addPaiSurveyCategory,
    listByPagePaiSurveyCategory,
    updatePaiSurveyCategory
    // addPaiSurveyCategoryLabel, listByPagePaiSurveyCategoryLabel, updatePaiSurveyCategoryLabel,
    // addPaiSurvey, listByPagePaiSurvey, updatePaiSurvey
  } from '@/api/survey'
  import SurveyCategoryLabel from './components/survey-category-label'

  const baseApi = process.env.VUE_APP_API_BASE_URL

  export default {
    components: {
      SurveyCategoryLabel
    },
    name: 'Index',
    data() {
      return {
        stateList: [
          { value: 1, label: '激活' },
          { value: 2, label: '冻结' }
        ],
        queryParam: {
          page: 1,
          limit: 20
        },
        total: 0,
        modal: {
          visible: false,
          visible1: false,
          currentId: '',
          title: '',
          form: {}
        },
        tableData: []
      }
    },
    mounted() {
      this.getTable()
    },
    methods: {
      query() {
        this.queryParam.page = 1
        this.getTable()
      },
      reset() {
        this.queryParam = {
          page: 1,
          limit: 20
        }
        this.getTable()
      },
      handleSizeChange(val) {
        this.queryParam.limit = val
        this.getTable()
      },
      handleCurrentChange(val) {
        this.queryParam.page = val
        this.getTable()
      },
      getTable() {
        listByPagePaiSurveyCategory(this.queryParam).then(res => {
          this.tableData = res.data.records
          this.total = parseInt(res.data.total)
        }).catch(() => {})
      },
      handleOk() {
        if (this.modal.form.id) {
          updatePaiSurveyCategory({ ...this.modal.form }).then(res => {
            this.$message.success(res.msg)
            this.modal.form = {}
            this.modal.visible = false
            this.queryParam.page = 1
            this.getTable()
          }).catch(() => {})
        } else {
          this.modal.form.state = 1
          addPaiSurveyCategory({ ...this.modal.form }).then(res => {
            this.$message.success(res.msg)
            this.modal.form = {}
            this.modal.visible = false
            this.queryParam.page = 1
            this.getTable()
          }).catch(() => {})
        }
      },
      handleAdd() {
        this.modal.form = {
          categoryName: '',
          categoryScore: '',
          sortOrder: '1',
          id: ''
        }
        this.modal.visible = true
        this.modal.title = '新增'
      },
      handleEdit(record) {
        const data = JSON.parse(JSON.stringify(record))
        this.modal.form = {
          categoryName: data.categoryName,
          categoryScore: data.categoryScore,
          sortOrder: data.sortOrder,
          id: data.id
        }
        this.modal.visible = true
        this.modal.title = '编辑'
      },

      handleLabel(row) {
        this.modal.currentId = row.id
        this.modal.visible1 = true
        this.modal.title = '编辑标签'
      }
    }
  }
</script>

<style lang="less" scoped>
  .view-video {
    position: relative;

    .close {
      position: absolute;
      right: 15px;
      top: 0;
      cursor: pointer;
    }
  }

  /deep/.ant-modal-body {
    max-height: calc(80vh - 150px);
    overflow-y: auto;

    &::-webkit-scrollbar {
      width: 6px;
      /*高宽分别对应横竖滚动条的尺寸*/
      height: 1px;
    }

    &::-webkit-scrollbar-thumb {
      // -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      background: #e3e3e6;
      border-radius: 6px;
    }

    &::-webkit-scrollbar-track {
      background: transparent;
      // -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      border-radius: 5px;
    }
  }
</style>